﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZG_member.Model;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ZG_member.DAL
{
    public class EmailDAL
    {
        public ZG_member.Model.EmailModel GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,emailUser,emailPwd from dbo.email");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            ZG_member.Model.EmailModel model = new ZG_member.Model.EmailModel();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"] != null && ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["emailUser"] != null && ds.Tables[0].Rows[0]["emailUser"].ToString() != "")
                {
                    model.EmailUser = ds.Tables[0].Rows[0]["emailUser"].ToString();
                }
                if (ds.Tables[0].Rows[0]["emailPwd"] != null && ds.Tables[0].Rows[0]["emailPwd"].ToString() != "")
                {
                    model.EmailPwd = ds.Tables[0].Rows[0]["emailPwd"].ToString();
                }
                
                return model;
            }
            else
            {
                return null;
            }
        
        }

        public bool Update(string emailUser, string emailPwd)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update email set ");
            strSql.Append("emailUser=@emailUser,emailPwd = @emailPwd ");
            SqlParameter[] parameters = {					
					new SqlParameter("@emailUser", SqlDbType.NVarChar,50),
                     new SqlParameter("@emailPwd", SqlDbType.NVarChar,50)};
            parameters[0].Value = emailUser;
            parameters[1].Value = emailPwd;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }



    }
}
